/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datos;

/**
 *
 * @author hjfxgh
 */
import java.sql.*;

public class Bd {

    private String user = "alejandro";
    private String password = "a1425lex";
    private String db = "divisionsubredes";
    private String host = "localhost";
    private String url;
    private Connection conn = null;
    private Statement stm;
    private ResultSet rs;
    private boolean exito = false;

    public boolean isExito() {
        return exito;
    }

    public Bd() {
        this.url = "jdbc:mysql://" + this.host + "/" + this.db;
        this.exito = this.connectar();
    }

    public Bd(String server, String usuario, String contraseña, String bd) {
        this.user = usuario;
        this.password = contraseña;
        this.db = bd;
        this.host = server;
        this.url = "jdbc:mysql://" + this.host + "/" + this.db;
        this.exito = this.connectar();
    }

    private boolean connectar() {
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
            conn = DriverManager.getConnection(url, user, password);
            if (conn != null) {
                System.out.println("Conexión a base de datos " + url + " … Ok");
                stm = conn.createStatement();
                return true;
            } else {
                return false;
            }
        } catch (SQLException ex) {
            System.out.println("Hubo un problema al intentar conectarse con la base de datos " + url);
            return false;
        } catch (ClassNotFoundException ex) {
            System.out.println(ex);
            return false;
        }
    }

    public String getDb() {
        return db;
    }

    public void setDb(String db) {
        this.db = db;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public ResultSet obtenerResultSet(String Query) throws SQLException {
        rs = stm.executeQuery(Query);
        return rs;
    }

    public boolean ejecutar(String Query) throws SQLException {
        try {
            stm.execute(Query);
            return true;
        } catch (SQLException ex) {
            return false;
        }
    }
}
